﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DrawTool : MonoBehaviour {

	public Transform target;
	private static LineRenderer GetLineRender(Transform t){
		LineRenderer lr = t.GetComponent<LineRenderer>();
		if (lr == null) {
			lr = t.gameObject.AddComponent<LineRenderer>();
		}
		lr.SetWidth(0.1f,0.1f);
		lr.material.color = Color.blue;
		return lr;
	}

	public static void DrawCircle(Transform t, Vector3 center, float radius){
		LineRenderer lr = GetLineRender(t);
		int pointAmount = 100;
		float eachAngle = 360f / pointAmount;
		Vector3 forward = t.forward;

		lr.SetVertexCount(pointAmount+1);

		for (int i = 0; i <= pointAmount; i++) {
			Vector3 pos = Quaternion.Euler(0f,eachAngle*i,0f)*forward*radius+center;
			lr.SetPosition(i,pos);
		}

	}

	//绘制空心扇形    
	public static void DrawSector(Transform t, Vector3 center, float angle, float radius)  
	{  
		LineRenderer lr = GetLineRender(t);  
		int pointAmount = 100;//点的数目，值越大曲线越平滑    
		float eachAngle = angle / pointAmount;  
		Vector3 forward = t.forward;  
		
		lr.SetVertexCount(pointAmount);  
		lr.SetPosition(0, center);  
		lr.SetPosition(pointAmount - 1, center);  
		
		for (int i = 1; i < pointAmount - 1; i++)  
		{  
			Vector3 pos = Quaternion.Euler(0f, -angle / 2 + eachAngle * (i - 1), 0f) * forward * radius + center;  
			lr.SetPosition(i, pos);  
		}  
	}  

	public static GameObject go;
	public static MeshFilter mf;
	public static MeshRenderer mr;
	public static Shader shader;
	public static GameObject CreateMesh(List<Vector3> vertices)
	{
		int [] triangles;
		Mesh mesh =new Mesh();

		int triangleAmount = vertices.Count - 2;
		triangles = new int[3 * triangleAmount];

		for (int i = 0; i < triangleAmount; i++) {
			triangles[3 * i] = 0;
			triangles[3 * i + 1] = i + 1;
			triangles[3 * i + 2] = i + 2;
		}

		if (go == null) {
			go = new GameObject("mesh");
			go.transform.position=new Vector3(0,0.1f,0);
			mf = go.AddComponent<MeshFilter>();
			mr = go.AddComponent<MeshRenderer>();
			shader = Shader.Find("Diffuse");
		}

		mesh.vertices = vertices.ToArray();
		mesh.triangles = triangles;

		mf.mesh = mesh;
		mr.material.shader = shader;
		mr.material.color = Color.red;

		return go;
	}

	//绘制实心圆    
	public static void DrawCircleSolid(Transform t, Vector3 center, float radius)  
	{  
		int pointAmount = 100;//点的数目，值越大曲线越平滑    
		float eachAngle = 360f / pointAmount;  
		Vector3 forward = t.forward;  
		
		List<Vector3> vertices = new List<Vector3>();  
		vertices.Add(center);
		for (int i = 0; i <= pointAmount; i++)  
		{  
			Vector3 pos = Quaternion.Euler(0f, eachAngle * i, 0f) * forward * radius + center;  
			vertices.Add(pos);  
		}  
		
		CreateMesh(vertices);  
	} 
	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButton(0)) {
			GetLineRender(target).enabled = true;
			DrawCircle(target,target.localPosition,3f);
			DrawCircleSolid(target,target.localPosition,2f);
		}else if(Input.GetMouseButton(1)){
			GetLineRender(target).enabled = true;
			DrawSector(target,target.localPosition,60,5f);
		}else{
			GetLineRender(target).enabled = false;
		}

	}
}
